﻿
function Gallery(rollHolder, galleryImgCatcher, imageTitle, callbackAfterSet) {

    var $gallery = this;
    $(function () {
        $gallery.RollHolder = $($gallery.RollHolder);
        $gallery.GalleryImageCatcher = $($gallery.GalleryImageCatcher);
        $gallery.ImageTitleHandler = $($gallery.ImageTitleHandler);

        $gallery.GalleryImageCatcher.hide();
        $gallery.SetPassedImages();
        $gallery.LoadRandomImageToPort();

        $gallery.RollHolder.click(function () {
            $gallery.OpenGallery();
        });
        if (callbackAfterSet) callbackAfterSet($gallery);
    });

    $(window).load(function () {
        $gallery.PreloadImages();
    });

    this.AttachToGalleryButton = function (jqButton) {
        var $gallery = this;

        $(jqButton).click(function (e) {

            e.preventDefault();
            e.stopPropagation();

            $gallery.OpenGallery();
        });
    }

    this.AttachToSchemeButton = function (category, jqButton) {
        var $gallery = this;

        var $images = new Array();
        var $titles = new Array();
        var $descriptions = new Array();

        category = category.toLowerCase();
        for (var xc = 0; xc < this.PrettyPhotoGalleryCategories.length; xc++) {
            if (this.PrettyPhotoGalleryCategories[xc].toLowerCase() == category) {
                $images.push($gallery.PrettyPhotoGalleryImages[xc]);
                $titles.push($gallery.PrettyPhotoGalleryTitles[xc]);
                $descriptions.push($gallery.PrettyPhotoGalleryDescriptions[xc]);
            }
        }

        if ($images.length > 0) {
            $(jqButton).click(function (e) {

                e.preventDefault();
                e.stopPropagation();

                $.fn.prettyPhoto({ theme: 'facebook', social_tools: false });
                $.prettyPhoto.open($images, $titles, $descriptions);
            });
        } else {
            $(jqButton).hide();
        }
    }

    this.OpenGallery = function () {
        $.fn.prettyPhoto({ theme: 'facebook', social_tools: false });
        $.prettyPhoto.open(this.PrettyPhotoGalleryImages, this.PrettyPhotoGalleryTitles, this.PrettyPhotoGalleryDescriptions);
    }

    this.__LoadNextFromCollection = function (collection, callbackOnEnd) {
        var $gallery = this;

        $('<img />')
            .load(function () {
                if (collection.length == 0)
                    callbackOnEnd();
                else
                    $gallery.__LoadNextFromCollection(collection, callbackOnEnd);
            })
            .attr('src', collection.pop());
    }

    this.PreloadImages = function () {

        var $prettyPhotoImages = new Array();
        $prettyPhotoImages.push('/prettyphoto/images/prettyPhoto/facebook/sprite.png');
        $prettyPhotoImages.push('/prettyphoto/images/prettyPhoto/facebook/btnNext.png');
        $prettyPhotoImages.push('/prettyphoto/images/prettyPhoto/facebook/btnPrevious.png');
        $prettyPhotoImages.push('/prettyphoto/images/prettyPhoto/facebook/contentPatternBottom.png');
        $prettyPhotoImages.push('/prettyphoto/images/prettyPhoto/facebook/contentPatternLeft.png');
        $prettyPhotoImages.push('/prettyphoto/images/prettyPhoto/facebook/contentPatternRight.png');
        $prettyPhotoImages.push('/prettyphoto/images/prettyPhoto/facebook/contentPatternTop.png');
        $prettyPhotoImages.push('/prettyphoto/images/prettyPhoto/facebook/loader.gif');
        $prettyPhotoImages.push('/prettyphoto/images/prettyPhoto/facebook/default_thumbnail.gif');
        $prettyPhotoImages.push('/prettyphoto/images/prettyPhoto/facebook/sprite.png');

        var $gallery = this;
        this.__LoadNextFromCollection($prettyPhotoImages, function () {

            $($gallery.PrettyPhotoGalleryImages).each(function (idx, imgSrc) {
                $('<img />').attr('src', this);
            });

        });
    }

    this.LoadRandomImageToPort = function () {
        var $gallery = this;
        var $idx = Math.floor(Math.random() * this.PassedImages.length);
        $('<img />').attr('src', this.PassedImages[$idx].replace('0x0', GalleryData.ApartSmGalleryParameterPhotoPortSize())).load(function () {
            $gallery.RollHolder.css('background-image', 'url(\'' + $(this).attr('src') + '\')');
            $gallery.ImageTitleHandler.text($gallery.PrettyPhotoGalleryCategories[$idx]);
            window.setTimeout("$('#" + $gallery.RollHolder.attr('id') + "').data('gallery').LoadRandomImageToPort();", GalleryData.ApartSmGalleryInterval());
        });
        this.RollHolder.data('gallery', this);
    }

    this.SetPassedImages = function () {
        var $gallery = this;

        // Tak, ładujmy zdjęcia w rozmiarze 80% rozmiaru monitora!
        var viewPort08Size = Math.floor($(window).width() * 0.8) + 'x' + Math.floor($(window).height() * 0.8);

        $gallery.PassedImages = new Array();
        this.GalleryImageCatcher.find('a[id$=lnkGallery]').each(function (idx, img) {
            $gallery.PassedImages.push($(img).attr('prototypeurl'));
            $gallery.PrettyPhotoGalleryImages.push($(img).attr('prototypeurl').replace('0x0', viewPort08Size));
            $gallery.PrettyPhotoGalleryCategories.push($(img).attr('gallerycategory'));
            $gallery.PrettyPhotoGalleryTitles.push($(img).attr('title'));
            $gallery.PrettyPhotoGalleryDescriptions.push($(img).attr('gallerydescription'));
        })
    }

    this.PassedImages = new Array();
    this.PrettyPhotoGalleryImages = new Array();
    this.PrettyPhotoGalleryCategories = new Array();
    this.PrettyPhotoGalleryTitles = new Array();
    this.PrettyPhotoGalleryDescriptions = new Array();
    this.RollHolder = rollHolder;
    this.GalleryImageCatcher = galleryImgCatcher;
    this.ImageTitleHandler = imageTitle;
}

var GalleryData = {

    ApartSmGalleryParameterThumbSize: function () {
        if (typeof window.apartSmGalleryParameterThumbSize != 'undefined')
            return window.apartSmGalleryParameterThumbSize;
        else
            return '100x100';
    },

    ApartSmGalleryParameterPhotoSize: function () {
        if (typeof window.apartSmGalleryParameterPhotoSize != 'undefined')
            return window.apartSmGalleryParameterPhotoSize;
        else
            return '600x450';
    },

    ApartSmGalleryParameterFullPhotoSize: function () {
        if (typeof window.apartSmGalleryParameterFullPhotoSize != 'undefined')
            return window.apartSmGalleryParameterFullPhotoSize;
        else
            return '1600x1200';
    },

    ApartSmGalleryParameterPhotoPortSize: function () {
        if (typeof window.apartSmGalleryParameterPhotoPortSize != 'undefined')
            return window.apartSmGalleryParameterPhotoPortSize;
        else
            return '456x342';
    },

    ApartSmGalleryInterval: function () {
        if (typeof window.apartSmGalleryInterval != 'undefined')
            return window.apartSmGalleryInterval;
        else
            return 5000;
    }

}

function SetGallery(pnlGalleryClientId) {
    var $gallery = new Gallery('#apartment-photo-port', pnlGalleryClientId, '.apartment-photo .black-gt30', function (gallery) {
        gallery.AttachToSchemeButton('schemat', $('.apartment-photo .ico-scheme').parent());
        gallery.AttachToGalleryButton($('.apartment-photo .ico-gallery').parent());
        $('.apartment-icons').click(function (e) { e.stopPropagation(); });
    });
}